package com.querie.beans;

import java.sql.*;
import java.util.*;


public class LoadPostgresData {


Connection lpConnection = null;


	public LoadPostgresData()
	{
	}


	public void getPostgresConnection() throws Exception{

		Class.forName("");
		this.lpConnection = DriverManager.getConnection("", "", "");
	}

	public ArrayList getResults(String sQuery) throws Exception{

			ArrayList aResults = new ArrayList();

			Statement s = lpConnection.createStatement();
			ResultSet rs = s.executeQuery(sQuery);
			while (rs.next())
			{
					for (int i = 0; i < rs.getMetaData().getColumnCount(); ++i) {
							aResults.add(rs.getString(i+1));

					}
			}
			rs.close();
			s.close();

			return aResults;
	}





	public static void main(String args[]){


		int iNoOfCols = 0;
		String sQuery = "";
		ArrayList aSessionIds = new ArrayList();
		ArrayList aErrorQueries = new ArrayList();
		ArrayList aUniqueQueries = new ArrayList();
		ArrayList aKeyValues = new ArrayList();
		List tupleIds = null;
		StringBuffer builder = new StringBuffer();
		QueryResults bQueryResults = new QueryResults();

		try {
			LoadPostgresData lp = new LoadPostgresData();
			lp.getPostgresConnection();

			//Get the Session Id's from PostGres
			sQuery = "select distinct sessionId from sessionqueries";
			aSessionIds = lp.getResults(sQuery);

			if(aSessionIds != null)
			{
				for(int i = 0; i < aSessionIds.size(); i++)
				{

					sQuery = "select distinct lower(queryString) from sessionqueries where sessionId ="+aSessionIds.get(i);
					aUniqueQueries = lp.getResults(sQuery);

					if(aUniqueQueries != null)
					{
						for(int j = 0; j < aUniqueQueries.size(); j++)
						{

							sQuery = aUniqueQueries.get(j).toString().toLowerCase();
							sQuery = sQuery.replaceAll("from", "\nfrom");

							bQueryResults.setSTxtQuery(sQuery);
							aKeyValues = bQueryResults.getPrimaryKeysOfTables();

							if(aKeyValues != null && aKeyValues.size() > 1 && aKeyValues.get(1).toString().trim().equals("ERROR"))
							{
								aErrorQueries.add(aSessionIds.get(i));
								aErrorQueries.add(sQuery);
							}
							else
							{


							tupleIds = new ArrayList();

								if(aKeyValues != null && aKeyValues.size() > 0)
								{
									iNoOfCols = Integer.parseInt(aKeyValues.get(0).toString());
									for (int k = iNoOfCols+1; k < aKeyValues.size(); k = k + iNoOfCols) {
										builder.setLength(0);

										for (int l = k; l < k + iNoOfCols; l++) {
											String tableName = aKeyValues.get(l % iNoOfCols).toString();
											String pkValue = aKeyValues.get(l).toString();
											builder.append(tableName.toUpperCase());
											builder.append("|");
											builder.append(pkValue);
										}

										long tupleId = builder.toString().hashCode();
										tupleIds.add(new Long(tupleId));
									}
								}

							MinHash.getInstance().findSimilarQueries(Integer.parseInt(aSessionIds.get(i).toString()), bQueryResults.getSCQuery(), tupleIds, null);
							}
						}
					}
				}
			}
//			lp.closeConnection();
                        lp.lpConnection.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}





	}
}